/**
 * 分数管理类
 */
export class ScoreManager {
    private score: number = 0;
    private lines: number = 0;
    private level: number = 1;

    // 分数计算规则
    private readonly SCORE_SINGLE = 100;
    private readonly SCORE_DOUBLE = 300;
    private readonly SCORE_TRIPLE = 500;
    private readonly SCORE_TETRIS = 800;
    private readonly LINES_PER_LEVEL = 10;

    /**
     * 处理消除行数的分数计算
     * @param linesCleared 消除的行数
     */
    addLines(linesCleared: number): void {
        if (linesCleared === 0) return;

        this.lines += linesCleared;

        // 根据消除行数计算分数
        let points = 0;
        switch (linesCleared) {
            case 1:
                points = this.SCORE_SINGLE;
                break;
            case 2:
                points = this.SCORE_DOUBLE;
                break;
            case 3:
                points = this.SCORE_TRIPLE;
                break;
            case 4:
                points = this.SCORE_TETRIS;
                break;
        }

        // 分数乘以当前等级
        this.score += points * this.level;

        // 检查是否升级
        const newLevel = Math.floor(this.lines / this.LINES_PER_LEVEL) + 1;
        if (newLevel > this.level) {
            this.level = newLevel;
        }
    }

    /**
     * 软下降加分（每下降一格）
     */
    addSoftDrop(): void {
        this.score += 1;
    }

    /**
     * 硬下降加分（一次到底）
     */
    addHardDrop(distance: number): void {
        this.score += distance * 2;
    }

    /**
     * 获取当前分数
     */
    getScore(): number {
        return this.score;
    }

    /**
     * 获取消除的行数
     */
    getLines(): number {
        return this.lines;
    }

    /**
     * 获取当前等级
     */
    getLevel(): number {
        return this.level;
    }

    /**
     * 重置分数
     */
    reset(): void {
        this.score = 0;
        this.lines = 0;
        this.level = 1;
    }
}

